//
//  MyLoanTableViewController.m
//  ZhuoxinAgriculture
//
//  Created by maxine on 2016/11/9.
//  Copyright © 2016年 KKING. All rights reserved.
//

#import "MyLoanTableViewController.h"
#import "MyLoadTableViewCell.h"
#import "ZXDetailTableViewController.h"
#import "MyLoanModel.h"

@interface MyLoanTableViewController ()<UITableViewDelegate>

@property (nonatomic,strong) NSMutableArray <MyLoanModel *>*dkManagerDatasource;

@property (nonatomic,strong) NSMutableArray *myDkDatasource;


@end

@implementation MyLoanTableViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    //    self.title = @"贷款管理";
    
    self.tableView.rowHeight = 111;
    [self.tableView registerNib:[UINib nibWithNibName:@"MyLoadTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyLoadTableViewCelldkManager"];
    
    
    [self getNetData];
    
}

- (void)getNetData{
    
    WeakSelf(Wself);
    [[ZXHTTPManager sharedManagerNOCer]smallDKloginWithCompleted:^(NSDictionary *jsonDict, NSError *error) {
        if (error) {
            NSLog(@"error = %@",error);
            HUDNormal(@"请检查您的网络");
            return ;
        }
        NSLog(@"jsonDict = %@",jsonDict[@"sessionId"]);
        [[NSUserDefaults standardUserDefaults]setValue:jsonDict[@"sessionId"] forKey:@"sessionId_key"];
        //业务代表 贷款管理
        if ([self.title isEqualToString:@"贷款管理"]) {
            [[ZXHTTPManager sharedManagerNOCer] mydkGuanliWithCompleted:^(NSDictionary *jsonDict, NSError *error) {
                
                if (error) {
                    NSLog(@"error = %@",error);
                    HUDNormal(@"请检查网络或稍后重试");
                    return ;
                }
                
                if ([jsonDict[@"statusCode"]isEqualToString:@"SUCCESS"]) {
                    NSLog(@"%@",jsonDict);
                    Wself.dkManagerDatasource =  [MyLoanModel mj_objectArrayWithKeyValuesArray:jsonDict[@"orders"]];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.tableView reloadData];
                    });
                }
            }];
        }
        //农民 我的贷款
        else if([self.title isEqualToString:@"我的贷款"]){
            [[ZXHTTPManager sharedManagerNOCer]mydkListWithCompleted:^(NSDictionary *jsonDict, NSError *error) {
                if (error) {
                    NSLog(@"error = %@",error);
                    HUDNormal(@"请检查网络或稍后重试");
                    return ;
                }
                if ([jsonDict[@"statusCode"]isEqualToString:@"SUCCESS"]) {
                    NSLog(@"%@",jsonDict);
                    Wself.myDkDatasource =  [MyLoanModel mj_objectArrayWithKeyValuesArray:jsonDict[@"orders"]];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.tableView reloadData];
                    });
                }
            }];
        }
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([self.title isEqualToString:@"贷款管理"]) {
        return self.dkManagerDatasource.count;
    }

    else if([self.title isEqualToString:@"我的贷款"]){
        return self.myDkDatasource.count;
    }
    return 0;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //创建cell
    MyLoadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyLoadTableViewCelldkManager" forIndexPath:indexPath];
    
    if ([self.title isEqualToString:@"贷款管理"]) {
        cell.model = self.dkManagerDatasource[indexPath.row];
    }

    else if([self.title isEqualToString:@"我的贷款"]){
        cell.model = self.myDkDatasource[indexPath.row];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZXDetailTableViewController *ZDVC = [[ZXDetailTableViewController alloc]init];
    if ([self.title isEqualToString:@"贷款管理"]) {
        ZDVC.model = self.dkManagerDatasource[indexPath.row];
    }
    
    else if([self.title isEqualToString:@"我的贷款"]){
        ZDVC.model = self.myDkDatasource[indexPath.row];
    }
    [self.navigationController pushViewController:ZDVC animated:YES];
    
}



/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
